package com.oa.pm.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.joneying.common.web.response.Resp;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.pm.entity.Organization;
import com.oa.pm.service.IOrganizationService;
import com.oa.pm.vo.OrganizationDetailsVO;
import com.oa.pm.vo.OrganizationTreeVO;
import com.oa.pm.vo.OrganizationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/organization")
@Api(description = "组织架构表模块")
//@ApiIgnore
public class OrganizationController extends BaseController {

    private final IOrganizationService organizationService;

    @Autowired
    public OrganizationController(IOrganizationService organizationService) {
        this.organizationService = organizationService;
    }

    /**
     * 新增
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp<OrganizationDetailsVO> findById(@ApiParam(value = "组织架构Id") @PathVariable String id) throws DataIsEmptyException {
        return organizationService.findOrganizationDetailsById(id);
    }


    /**
     * 新增
     *
     * @param organization Organization
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "组织架构表对象") @RequestBody(required = false) Organization organization) {
        organization.setCreateTime(new Date());
        organization.setCreateId(getLoginUserId());
        if (organization.getPId() == null) {
            organization.setPId("0");
        }
        if (organizationService.insert(organization)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id修改
     *
     * @param organization Organization
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "组织架构表对象") @RequestBody(required = false) Organization organization) {
        if (organizationService.updateById(organization)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        Organization organization = organizationService.selectById(id);
        if (organization != null && organization.getIsDeleted() == 2) {
            organization.setIsDeleted(1);
            if (organizationService.updateById(organization)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }


    @GetMapping("/tree")
    @CommonController
    @Description("查询组织架构树")
    @ApiOperation(value = "查询组织架构树")
    public Resp<List<OrganizationTreeVO>> findOrganizationTree(
            @ApiParam("组织架构名称") @RequestParam(value = "organizationName", required = false) String organizationName,
            @ApiParam("父级ID") @RequestParam(value = "pId", required = false) String pId) {
        return new Resp().success(organizationService.findOrganizationTree(organizationName, pId));
    }

    @GetMapping("/user/tree")
    @CommonController
    @Description("查询组织架构树")
    @ApiOperation(value = "查询组织架构树")
    public Resp<List<OrganizationTreeVO>> findOrganizationUserTree() {
        return new Resp().success(organizationService.findOrganizationUserTree());
    }

    @GetMapping("/list")
    @CommonController
    @Description("查询部门")
    @ApiOperation(value = "查询部门")
    public Resp<List<OrganizationVo>> list() {
        Wrapper wrapper = new EntityWrapper();
        wrapper.where("is_deleted = 2");

        List<OrganizationVo> organizationVos = new ArrayList<>();

        List<Organization> organization = organizationService.selectList(wrapper);
        organization.forEach(d -> {
            OrganizationVo organizationVo = new OrganizationVo();
            BeanUtils.copyProperties(d, organizationVo);
            organizationVos.add(organizationVo);
        });

        return new Resp<List<OrganizationVo>>().success(organizationVos);
    }

    @GetMapping("/department/list")
    @CommonController
    @Description("查询部门")
    @ApiOperation(value = "查询部门")
    public Resp<List<OrganizationVo>> findDepartment() {

        List<OrganizationVo> organizationVos = new ArrayList<>();

        Wrapper wrapper = new EntityWrapper();
        wrapper.where("p_id in (select id from pm_organization where p_id = '0') and is_deleted = 2");

        List<Organization> organization = organizationService.selectList(wrapper);
        organization.forEach(d -> {
            OrganizationVo organizationVo = new OrganizationVo();
            BeanUtils.copyProperties(d, organizationVo);
            organizationVos.add(organizationVo);
        });

        return new Resp<List<OrganizationVo>>().success(organizationVos);
    }

    @GetMapping("/company/list")
    @CommonController
    @Description("查询公司")
    @ApiOperation(value = "查询公司")
    public Resp<List<OrganizationVo>> findCompany() {

        List<OrganizationVo> organizationVos = new ArrayList<>();

        List<Organization> organization = organizationService.selectList(new EntityWrapper().eq("p_id", "0").eq("is_deleted", 2));
        organization.forEach(d -> {
            OrganizationVo organizationVo = new OrganizationVo();
            BeanUtils.copyProperties(d, organizationVo);
            organizationVos.add(organizationVo);
        });

        return new Resp<List<OrganizationVo>>().success(organizationVos);
    }
}
